//
//  PrentView.swift
//  PicPost
//
//  Created by dream on 2024/1/9.
//

import UIKit

class PrentView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    private lazy var button : UIButton = {
        let button = UIButton(type: .custom)
        return button
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        mj_w = UIScreen.main.bounds.width
        mj_h = UIScreen.main.bounds.height
        
        button.frame = bounds
        insertSubview(button, at: 0)
        
        button.addTarget(self, action: #selector(buttonTap), for: .touchUpInside)
    }
    
    @objc func buttonTap() {
        hide()
    }
    
//    override func hide() {
//        getCurrentVc().dismiss(animated: true)
//    }
}



public func getCurrentVc() -> UIViewController{
    let rootVc = UIApplication.shared.keyWindow?.rootViewController
    let currentVc = getCurrentVcFrom(rootVc!)
    return currentVc
}


private func getCurrentVcFrom(_ rootVc:UIViewController) -> UIViewController{
    var currentVc:UIViewController
    var rootCtr = rootVc
    if rootVc.isKind(of:UITabBarController.classForCoder()) {
        currentVc = getCurrentVcFrom((rootVc as! UITabBarController).selectedViewController!)
    }else if rootVc.isKind(of:UINavigationController.classForCoder()){
        currentVc = getCurrentVcFrom((rootVc as! UINavigationController).visibleViewController!)
    }else{
        if(rootCtr.presentedViewController != nil) {
            currentVc = getCurrentVcFrom(rootVc.presentedViewController!)
        }else {
            currentVc = rootCtr
        }
    }
    return currentVc
}
